package com.huawei.hms.push;

import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.huawei.hms.common.data.DataBufferSafeParcelable;
import com.huawei.hms.push.c.d;
import com.huawei.hms.support.api.push.PushException;
import com.huawei.hms.support.log.HMSLog;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONException;
import org.json.JSONObject;

public final class RemoteMessage implements Parcelable {
    public static final Creator<RemoteMessage> CREATOR = new b();
    public static final long DEFAULT_SEND_TIME = 0;
    public static final int DEFAULT_TTL = 0;
    public static final int PRIORITY_HIGH = 1;
    public static final int PRIORITY_LOW = 2;
    public static final int PRIORITY_NORMAL = 0;
    public static final int PRIORITY_UNKNOWN = 3;
    private Bundle a;
    private Notification b;

    public static class Builder {
        private final Bundle a = new Bundle();
        private final Map<String, String> b = new HashMap();

        public Builder(String str) {
            this.a.putString("to", str);
        }

        public RemoteMessage build() {
            Bundle bundle = new Bundle();
            JSONObject jSONObject = new JSONObject();
            try {
                for (Entry entry : this.b.entrySet()) {
                    jSONObject.put((String) entry.getKey(), entry.getValue());
                }
                try {
                    String string = this.a.getString("msgId");
                    String jSONObject2 = jSONObject.toString();
                    JSONObject jSONObject3 = new JSONObject();
                    jSONObject3.put("collapseKey", this.a.getString("collapseKey"));
                    jSONObject3.put("ttl", this.a.getString("ttl"));
                    JSONObject jSONObject4 = new JSONObject();
                    if (jSONObject.length() != 0) {
                        jSONObject4.put(DataBufferSafeParcelable.DATA_FIELD, jSONObject2);
                    }
                    jSONObject4.put("msgId", string);
                    jSONObject3.put("msgContent", jSONObject4);
                    bundle.putByteArray("message_body", jSONObject3.toString().getBytes("UTF-8"));
                    bundle.putString("to", this.a.getString("to"));
                    bundle.putString("message_type", this.a.getString("message_type"));
                    return new RemoteMessage(bundle);
                } catch (UnsupportedEncodingException e) {
                    HMSLog.w("RemoteMessage", "convert message body error, unexpect encoding or json contend.");
                    throw new PushException(PushException.EXCEPTION_SEND_FAILED);
                } catch (JSONException e2) {
                    HMSLog.w("RemoteMessage", "convert message body error, unexpect encoding or json contend.");
                    throw new PushException(PushException.EXCEPTION_SEND_FAILED);
                }
            } catch (JSONException e3) {
                HMSLog.w("RemoteMessage", "convert data map to json error, unexpect json contend.");
                throw new PushException(PushException.EXCEPTION_SEND_FAILED);
            }
        }

        public Builder addData(String str, String str2) {
            if (str == null) {
                throw new IllegalArgumentException("Missing 'data'");
            }
            this.b.put(str, str2);
            return this;
        }

        public Builder setData(Map<String, String> map) {
            this.b.clear();
            for (Entry entry : map.entrySet()) {
                this.b.put(entry.getKey(), entry.getValue());
            }
            return this;
        }

        public Builder clearData() {
            this.b.clear();
            return this;
        }

        public Builder setMessageId(String str) {
            this.a.putString("msgId", str);
            return this;
        }

        public Builder setMessageType(String str) {
            this.a.putString("message_type", str);
            return this;
        }

        public Builder setTtl(int i) {
            this.a.putString("ttl", String.valueOf(i));
            return this;
        }

        public Builder setCollapseKey(String str) {
            this.a.putString("collapseKey", str);
            return this;
        }
    }

    @Retention(RetentionPolicy.SOURCE)
    public @interface MessagePriority {
    }

    public static class Notification implements Serializable {
        private final String a;
        private final String b;
        private final String[] c;
        private final String d;
        private final String e;
        private final String[] f;
        private final String g;
        private final String h;
        private final String i;
        private final String j;
        private final String k;
        private final String l;
        private final Uri m;
        private final int n;

        private Notification(Bundle bundle) {
            this.a = bundle.getString("notifyTitle");
            this.d = bundle.getString("content");
            this.b = bundle.getString("title_loc_key");
            this.e = bundle.getString("body_loc_key");
            this.c = bundle.getStringArray("title_loc_args");
            this.f = bundle.getStringArray("body_loc_args");
            this.g = bundle.getString("icon");
            this.j = bundle.getString("color");
            this.h = bundle.getString("sound");
            this.i = bundle.getString("tag");
            this.l = bundle.getString("channelId");
            this.k = bundle.getString("intentUri");
            this.n = bundle.getInt("notifyId");
            Object string = bundle.getString("url");
            this.m = !TextUtils.isEmpty(string) ? Uri.parse(string) : null;
        }

        public String getTitle() {
            return this.a;
        }

        public String getTitleLocalizationKey() {
            return this.b;
        }

        public String[] getTitleLocalizationArgs() {
            return this.c == null ? null : (String[]) this.c.clone();
        }

        public String getBody() {
            return this.d;
        }

        public String getBodyLocalizationKey() {
            return this.e;
        }

        public String[] getBodyLocalizationArgs() {
            return this.f == null ? null : (String[]) this.f.clone();
        }

        public String getIcon() {
            return this.g;
        }

        public Uri getImageUrl() {
            return null;
        }

        public String getSound() {
            return this.h;
        }

        public String getTag() {
            return this.i;
        }

        public String getColor() {
            return this.j;
        }

        public String getClickAction() {
            return this.k;
        }

        public String getChannelId() {
            return this.l;
        }

        public Uri getLink() {
            return this.m;
        }

        public int getNotifyId() {
            return this.n;
        }
    }

    public RemoteMessage(Bundle bundle) {
        this.a = a(bundle);
    }

    public RemoteMessage(Parcel parcel) {
        this.a = parcel.readBundle();
        this.b = (Notification) parcel.readSerializable();
    }

    private Bundle a(Bundle bundle) {
        Bundle bundle2 = new Bundle();
        JSONObject b = b(bundle);
        JSONObject a = a(b);
        String a2 = d.a(a, DataBufferSafeParcelable.DATA_FIELD, null);
        if (bundle.getInt("inputType") == 1 && a(a, a2)) {
            try {
                bundle2.putString(DataBufferSafeParcelable.DATA_FIELD, new String(bundle.getByteArray("message_body"), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                HMSLog.w("RemoteMessage", "get data body error, unsupport encoding.");
            }
            return bundle2;
        }
        JSONObject b2 = b(a);
        JSONObject c = c(b2);
        JSONObject d = d(b2);
        String string = bundle.getString("to");
        String a3 = d.a(b, "from", null);
        String a4 = d.a(a, "msgId", null);
        String a5 = d.a(b, "collapseKey", null);
        String a6 = d.a(b, "sendTime", null);
        int a7 = d.a(b, "ttl", 0);
        int a8 = d.a(b, "priority", 0);
        bundle2.putString("from", a3);
        bundle2.putString("to", string);
        bundle2.putString(DataBufferSafeParcelable.DATA_FIELD, a2);
        bundle2.putString("msgId", a4);
        bundle2.putString("collapseKey", a5);
        bundle2.putString("sendTime", a6);
        bundle2.putInt("ttl", a7);
        bundle2.putInt("priority", a8);
        bundle2.putBundle("notification", a(b, a, b2, c, d));
        return bundle2;
    }

    private boolean a(JSONObject jSONObject, String str) {
        return jSONObject == null || TextUtils.isEmpty(str);
    }

    private Bundle a(JSONObject jSONObject, JSONObject jSONObject2, JSONObject jSONObject3, JSONObject jSONObject4, JSONObject jSONObject5) {
        Bundle bundle = new Bundle();
        String a = d.a(jSONObject3, "notifyTitle", null);
        String a2 = d.a(jSONObject3, "content", null);
        String a3 = d.a(jSONObject3, "title_loc_key", null);
        String a4 = d.a(jSONObject3, "body_loc_key", null);
        String a5 = d.a(jSONObject3, "notifyIcon", null);
        String[] a6 = d.a(jSONObject3, "title_loc_args", null);
        String[] a7 = d.a(jSONObject3, "body_loc_args", null);
        String a8 = d.a(jSONObject4, "icon", null);
        String a9 = d.a(jSONObject4, "color", null);
        String a10 = d.a(jSONObject4, "sound", null);
        String a11 = d.a(jSONObject, "tag", null);
        String a12 = d.a(jSONObject, "channelId", null);
        String a13 = d.a(jSONObject5, "intentUri", null);
        String a14 = d.a(jSONObject5, "url", null);
        int a15 = d.a(jSONObject2, "notifyId", 0);
        bundle.putString("notifyTitle", a);
        bundle.putString("title_loc_key", a3);
        bundle.putString("content", a2);
        bundle.putString("body_loc_key", a4);
        bundle.putString("icon", a8);
        bundle.putString("notifyIcon", a5);
        bundle.putString("sound", a10);
        bundle.putString("tag", a11);
        bundle.putString("color", a9);
        bundle.putString("intentUri", a13);
        bundle.putString("channelId", a12);
        bundle.putString("url", a14);
        bundle.putInt("notifyId", a15);
        bundle.putStringArray("title_loc_args", a6);
        bundle.putStringArray("body_loc_args", a7);
        return bundle;
    }

    public String getFrom() {
        return this.a.getString("from");
    }

    public String getTo() {
        return this.a.getString("to");
    }

    public String getData() {
        return this.a.getString(DataBufferSafeParcelable.DATA_FIELD);
    }

    public String getCollapseKey() {
        return this.a.getString("collapseKey");
    }

    public String getMessageId() {
        return this.a.getString("msgId");
    }

    public String getMessageType() {
        return null;
    }

    public long getSentTime() {
        long j = 0;
        try {
            Object string = this.a.getString("sendTime");
            if (!TextUtils.isEmpty(string)) {
                j = Long.parseLong(string);
            }
        } catch (NumberFormatException e) {
            HMSLog.w("RemoteMessage", "NumberFormatException: get sendTime error.");
        }
        return j;
    }

    public int getTtl() {
        return this.a.getInt("ttl");
    }

    public int getOriginalPriority() {
        return 0;
    }

    public int getPriority() {
        return this.a.getInt("priority");
    }

    public Notification getNotification() {
        Bundle bundle = this.a.getBundle("notification");
        if (this.b == null && bundle != null) {
            this.b = new Notification(bundle);
        }
        if (this.b == null) {
            this.b = new Notification(new Bundle());
        }
        return this.b;
    }

    private static JSONObject b(Bundle bundle) {
        try {
            return new JSONObject(new String(bundle.getByteArray("message_body"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        } catch (JSONException e2) {
        }
        HMSLog.w("RemoteMessage", "parse message body error, unsupport encoding or unexpect json contend.");
        return null;
    }

    private static JSONObject a(JSONObject jSONObject) {
        if (jSONObject != null) {
            try {
                return jSONObject.getJSONObject("msgContent");
            } catch (JSONException e) {
                HMSLog.w("RemoteMessage", "parse msgContent error, unexpect json contend.");
            }
        }
        return null;
    }

    private static JSONObject b(JSONObject jSONObject) {
        if (jSONObject != null) {
            try {
                return jSONObject.getJSONObject("psContent");
            } catch (JSONException e) {
                HMSLog.w("RemoteMessage", "parse psContent error, unexpect json contend.");
            }
        }
        return null;
    }

    private static JSONObject c(JSONObject jSONObject) {
        if (jSONObject != null) {
            try {
                return jSONObject.getJSONObject("notifyDetail");
            } catch (JSONException e) {
                HMSLog.w("RemoteMessage", "parse notifyDetail error, unexpect json contend.");
            }
        }
        return null;
    }

    private static JSONObject d(JSONObject jSONObject) {
        if (jSONObject != null) {
            try {
                return jSONObject.getJSONObject("param");
            } catch (JSONException e) {
                HMSLog.w("RemoteMessage", "parse param error, unexpect json contend.");
            }
        }
        return null;
    }

    public final int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeBundle(this.a);
        parcel.writeSerializable(this.b);
    }
}
